﻿using System;
using System.Collections.Generic;
using System.Linq;
using BSU.CSCE.Data.Filters;
using BSU.CSCE.Data.Model;
using BSU.CSCE.Data.Repositories.Interfaces;

namespace BSU.CSCE.Data.Repositories
{
    public class PublicationRepository : BaseRepository<Publication, int>, IPublicationRepository
    {
        #region IPublicationRepository Members

        public virtual List<Publication> ListPublished(PublicationType type, string sortPath, bool asceding, int startIndex, int count, out int totalCount)
        {
            
            CSCEDataContext context = GetNewContext();

            IQueryable<Publication> publications = context.Publications;
            
            publications = publications.Where(x => x.PublishingTime <= DateTime.Now);
            publications = publications.Where(x => x.Status == PublicationStatus.Active);

            return publications
                .Sort(sortPath, asceding)
                .Page(startIndex, count, out totalCount).ToList();
        }

        #endregion
    }
}
